<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>活动信息</title>
</head>
<body>
    <h1>后台</h1>

    <div>
        <button onclick="createActivity()">创建秒杀活动</button>
        <button onclick="startActivity()">开始秒杀活动</button>
        <button onclick="endActivity()">结束秒杀活动</button>
    </div>

    <h2>秒杀活动</h2>
    <div>
        <p>活动名称：<span id="name"></span></p>
        <p>商品编号：<span id="code"></span></p>
        <p>活动价格：<span id="price"></span></p>
        <p>活动库存：<span id="stock"></span></p>
        <p>限购：<span id="limit"></span></p>
        <p>活动开始时间：<span id="startTime"></span></p>
        <p>活动结束时间：<span id="endTime"></span></p>
        <p>状态：<span id="status"></span></p>
    </div>

<script th:src="@{/js/jquery-3.6.0.js}"></script>
<script th:src="@{/js/axios.min.js}"></script>
<script th:src="@{/js/common.js}"></script>

<script>
    const code = "1000";
    $(document).ready(queryActivity);

    function queryActivity() {
        const url = API["activityData"];
        const params = {code: code};
        get(url, params, function(result) {
            if (result.code == RESULT_OK) {
                const data = result.data;
                $("#name").text(data.name);
                $("#code").text(data.code);
                $("#price").text((data.price/100).toFixed(2));
                $("#stock").text(data.stock);
                $("#limit").text(data.limit);
                $("#startTime").text(data.startTime);
                $("#endTime").text(data.endTime);
                $("#status").text(data.status);
            }
        })
    }

    function createActivity() {
        const url = API["createActivity"];
        const params = {};
        post(url, params, function(result) {
            if (result.code == RESULT_OK) {
                location.reload();
            } else {
                alert("创建活动失败");
            }
        })
    }

    function startActivity() {
        const url = API["startActivity"];
        const params = {code: code};
        post(url, params, function(result){
            if (result.code == RESULT_OK) {
                location.reload();
            } else {
                alert("开始活动失败");
            }
        })
    }

    function endActivity() {
        const url = API["endActivity"];
        const params = {code: code};
        post(url, params, function(result) {
            if (result.code == RESULT_OK) {
                location.reload();
            } else {
                alert("结束活动失败");
            }
        })
    }
</script>
</body>
</html>